package com.ling.simul.utils;

import com.ling.simul.enums.ResultCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 统一返回格式类
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class R {

    /**
     * 状态码
     */
    private Integer code;
 
    /**
     * 返回的消息
     */
    private String message;
 
    /**
     * 放置响应的数据
     */
    private Object data;
 
    /** 以下是定义一些常用到的格式，可以看到调用了我们创建的枚举类 */
    
    public static R ok() {
        R r = new R();
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }
 
    public static R error() {
        R r = new R();
        r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode());
        r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage());
        return r;
    }
 
    public R message(String message) {
        this.setMessage(message);
        return this;
    }
 
    public R data(Object data) {
        this.setData(data);
        return this;
    }
}